package com.blb.redis.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.redis.entity.User;
import com.blb.redis.mapper.UserMapper;
import com.blb.redis.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xray
 * @since 2023-01-31
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
//    public static final String KEY =  "User-";
//
//    //注入RedisTemplate对象
//    @Autowired
//    private RedisTemplate<String , Object> redisTemplate;

    @Cacheable(cacheNames = "User" , key = "T(String).valueOf(#id)")
    @Override
    public User getUserById(Long id) {
        return this.getById(id);
    }

//    @Override
//    public User getUserById(Long id) {
//        //获得string类型的操作对象
//        ValueOperations<String , Object> ops = redisTemplate.opsForValue();
//        //查询Redis中的用户
//        User user = (User) ops.get(KEY + id);
//        if(user == null) {
//            log.info("rides里没有该用户");
//            user = this.getById(id);
//            if(user != null) {
//                log.info("在数据库查到保存在Redis中");
//                ops.set(KEY + id, user);
//                return user;
//            }
//        }else {
//            log.info("rides查到，返回对象");
//            return user;
//        }
//        log.info("数据库里也没有 返回null");
//        return null;
//    }

}
